package org.skatiger.jrove.spring.tags;

import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.component.html.HtmlComponentBase;
import org.skatiger.jrove.spring.view.JroveView;
import org.skatiger.jrove.util.AssertUtil;
import org.springframework.web.servlet.support.RequestContext;

public abstract class SpringComponent extends HtmlComponentBase {
    private RequestContext requestContext;
    private Boolean _htmlEscape;

    /**
     * Return the HTML escaping setting for this tag, or the default setting if
     * not overridden.
     * 
     * @see #isDefaultHtmlEscape()
     */
    public boolean isHtmlEscape() {
        if (this._htmlEscape != null) {
            return this._htmlEscape.booleanValue();
        } else {
            return isDefaultHtmlEscape();
        }
    }

    public void setHtmlEscape(boolean htmlEscape) {
        this._htmlEscape = htmlEscape;
    }

    /**
     * Return the applicable default HTML escape setting for this tag.
     * <p>
     * The default implementation checks the RequestContext's setting, falling
     * back to <code>false</code> in case of no explicit default given.
     * 
     * @see #getRequestContext()
     */
    protected boolean isDefaultHtmlEscape() {
        return getRequestContext().isDefaultHtmlEscape();
    }

    /**
     * Return the current RequestContext.
     */
    public final RequestContext getRequestContext() {
        if (this.requestContext == null) {
            this.requestContext = getRequestContext(this.getViewContext());
        }
        return requestContext;
    }

    public final RequestContext getRequestContext(ViewContext viewContext) {
        RequestContext _requestContext = (RequestContext) viewContext
                .getAttribute(JroveView.REQUEST_CONTEXT_VIEW_ATTRIBUTE);
        AssertUtil.notNull(_requestContext,
                "RequestContext not found in ViewContext.");
        return _requestContext;
    }
}
